package com.ceba.test.modules.admin.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ceba.base.helper.DBJSONHelper;
import com.ceba.test.base.helper.SystemHelper;
import com.ceba.test.base.utils.StringUtil;
import com.ceba.test.modules.admin.entity.QfRule;
import org.json.JSONArray;
import org.json.JSONObject;

import com.ceba.base.web.dao.IBaseDao;
import com.ceba.test.modules.admin.entity.GReportMerge;
import com.ceba.test.modules.admin.entity.ViewFondreReportMerge;

import abc.data.DataRow;
import abc.data.DataTable;
import abc.data.Sql;
import abc.data.SqlSessionManager;
import abc.data.dataSource.SqlDataSource;
import abc.data.store.IStore;
import abc.data.store.StoreManager;
import abc.object.IObject;
import abc.object.XObject;
import org.omg.CORBA.PUBLIC_MEMBER;

public class GReportMergeDao extends IBaseDao{
	/**
	 * 根据id获取
	 * @param id
	 * @return
	 * @throws Exception
	 */
	public GReportMerge getReportMergeById(String id) throws Exception{
		DataTable dt=SqlSessionManager.getDataTable("SELECT * FROM g_report_merge where ID='"+id+"'");
		if(dt !=null && dt.getRowCount() >0){
			DataRow dr = dt.getRow(0);
			GReportMerge gm = new GReportMerge();
			gm.setId(dr.getString("ID"));
			gm.setCoverFileName(dr.getString("COVER_FILE_NAME"));
			gm.setCoverFilePath(dr.getString("COVER_FILE_PATH"));
			gm.setCoverFileUrl(dr.getString("COVER_FILE_URL"));
			gm.setFooterFileName(dr.getString("FOOTER_FILE_NAME"));
			gm.setFooterFilePath(dr.getString("FOOTER_FILE_PATH"));
			gm.setFooterFileUrl(dr.getString("FOOTER_FILE_URL"));
			gm.setStorageAddressId(dr.getString("STORAGE_ADDRESS_ID"));
			gm.setName(dr.getString("NAME"));
			return gm;
		}
		return null;
	}

	/*
	 * 根据id删除
	 */
	public boolean deleteReportMergeById(String id) {
		Sql sql = new Sql("delete from g_report_merge where ID='"+id+"'");
		return SqlSessionManager.execute(sql);
	}

	public boolean updateReportMerge(GReportMerge rm) {
		IStore store = StoreManager.create("g_report_merge");
		XObject object = new XObject("ID",rm.getId());
		object.put("NAME", rm.getName());
		object.put("COVER_FILE_NAME", rm.getCoverFileName() );
		object.put("COVER_FILE_PATH", rm.getCoverFilePath());
		object.put("COVER_FILE_URL", rm.getCoverFileUrl());
		object.put("STORAGE_ADDRESS_ID",rm.getStorageAddressId());
		object.put("FOOTER_FILE_NAME", rm.getFooterFileName() );
		object.put("FOOTER_FILE_PATH", rm.getFooterFilePath());
		object.put("FOOTER_FILE_URL", rm.getFooterFileUrl());

		if (!StringUtil.isEmptyString(rm.getStorageAddressId())) {
			object.put("STORAGE_ADDRESS_ID", rm.getStorageAddressId());
		}

		if(store.update(object)>0){
			return true;
		}
		return false;
	}

	
	public boolean insertReportMerge(GReportMerge rm) {
		IStore store = StoreManager.create("g_report_merge");
		XObject object = new XObject("ID",rm.getId());
		object.put("NAME", rm.getName());
		object.put("COVER_FILE_NAME", rm.getCoverFileName() );
		object.put("COVER_FILE_PATH", rm.getCoverFilePath());
		object.put("COVER_FILE_URL", rm.getCoverFileUrl());
		object.put("STORAGE_ADDRESS_ID",rm.getStorageAddressId());
		object.put("FOOTER_FILE_NAME", rm.getFooterFileName() );
		object.put("FOOTER_FILE_PATH", rm.getFooterFilePath());
		object.put("FOOTER_FILE_URL", rm.getFooterFileUrl());
		object.setCreateTime();
		if(store.insert(object)>0){
			return true;
		}
		return false;
	}

	/**
	 * 根据名称查找
	 * @param name
	 * @return
	 */
	public boolean checkReportMergeByName(String name,String id) {
		DataTable dt=SqlSessionManager.getDataTable("SELECT NAME FROM g_report_merge where NAME='"+name+"' and ID <> '"+id+"'");
		if(dt !=null && dt.getRowCount() >0){
			return true;
		}
		return false;
	}

	
	public String getReportMergeList(String key, Map<String,Object> dic){
		SqlDataSource dataSource = new SqlDataSource();
		Sql sql = null;
		try {
			 String start = (String)dic.get("start");
			 String limit = (String)dic.get("li");
			 if(start !=null && limit !=null){
				 sql = new Sql("SELECT * from  g_report_merge WHERE NAME like '%"+key+"%' OR COVER_FILE_NAME like '%"+key+"%' OR FOOTER_FILE_NAME like '%"+key+"%' limit "+dic.get("start")+","+dic.get("limit")+" ORDER BY CREATE_TIME DESC");
			 }else{
				 sql = new Sql("SELECT * from  g_report_merge WHERE NAME like '%"+key+"%' OR COVER_FILE_NAME like '%"+key+"%' OR FOOTER_FILE_NAME like '%"+key+"%' ORDER BY CREATE_TIME DESC");
			 }
			 DataTable dt = SqlSessionManager.getDataTable(sql);
			if(dt !=null && dt.size() > 0 ){
				 return dt.toJSON(); 
			}else{
				return DBJSONHelper.getEmptyDBJSONString();
			}
		} catch (Exception e) {
			return DBJSONHelper.getEmptyDBJSONString();
		}
	}

	public long getReportMergeCount(String key, Map<String,Object> dic){
		SqlDataSource dataSource = new SqlDataSource();
		Sql sql = null;
		try {
			 sql = new Sql("SELECT * from  g_report_merge WHERE NAME like '%"+key+"%' OR COVER_FILE_NAME like '%"+key+"%' OR FOOTER_FILE_NAME like '%"+key+"%'");
			 long dt = SqlSessionManager.getCount(sql);
			 return dt;
		} catch (Exception e) {
			return 0;
		}
	}

	public void parseDrToStamp(DataRow dr,GReportMerge merge){
		merge.setCoverFileName(dr.getString("COVER_FILE_NAME"));
		merge.setCoverFilePath(dr.getString("COVER_FILE_PATH"));
		merge.setFooterFileName(dr.getString("FOOTER_FILE_NAME"));
		merge.setName(dr.getString("NAME"));
		merge.setId(dr.getString("ID"));
		merge.setFooterFileUrl(dr.getString("FOOTER_FILE_URL"));
		merge.setCoverFileUrl(dr.getString("COVER_FILE_URL"));
		merge.setFooterFilePath(dr.getString("FOOTER_FILE_PATH"));
		merge.setStorageAddressId(dr.getString("STORAGE_ADDRESS_ID"));
	}

	public List<GReportMerge> getReportMergeListOne(String key){
		SqlDataSource dataSource = new SqlDataSource();
		Sql sql = null;
		try {
			if(key == null){
				key="";
			}
			 sql = new Sql("SELECT * from  g_report_merge WHERE NAME like '%"+key+"%' OR COVER_FILE_NAME like '%"+key+"%' OR FOOTER_FILE_NAME like '%"+key+"%'  ");
			DataTable dt = SqlSessionManager.getDataTable(sql);
			List<GReportMerge> list = new ArrayList<GReportMerge>();
			if(dt !=null && dt.getRowCount()>0){
				for(int i = 0 ; i < dt.getRowCount() ; i ++){
					DataRow dr = dt.getRow(i);
					GReportMerge  gReportMerge= new GReportMerge();
					parseDrToStamp(dr, gReportMerge);
					list.add(gReportMerge);
				}
			}
			return list;
		} catch (Exception e) {
			return null;
		}
	}

	//文件封面尾页授权查询
	public List<GReportMerge> getCoverEndListSqlUserId(String userId) throws Exception{
		SqlDataSource dataSource = new SqlDataSource();

		Sql sql = new Sql("SELECT gr.ID,gr.NAME, gr.COVER_FILE_PATH,gr.STORAGE_ADDRESS_ID,gr.COVER_FILE_URL, gr.COVER_FILE_NAME,gr.FOOTER_FILE_NAME, gr.FOOTER_FILE_URL,gr.FOOTER_FILE_PATH FROM g_report_merge as gr"
				+ "	LEFT OUTER JOIN g_user_cor_stamp as gu ON gu.USER_AUTHORIZATION_ID = gr.ID "
				+ " WHERE gu.USER_ID = '"+ userId + "'and gu.AUTHORIZATION_TYPE='3' "
				+ " GROUP BY gr.NAME,gr.ID,gr.COVER_FILE_PATH,gr.STORAGE_ADDRESS_ID,gr.COVER_FILE_URL, gr.COVER_FILE_NAME,gr.FOOTER_FILE_NAME, gr.FOOTER_FILE_URL,gr.FOOTER_FILE_PATH");
		DataTable dt = SqlSessionManager.getDataTable(sql);
		List<GReportMerge> list = new ArrayList<GReportMerge>();
		if(dt !=null && dt.getRowCount()>0){
			for(int i = 0 ; i < dt.getRowCount() ; i ++){
				DataRow dr = dt.getRow(i);
				GReportMerge gReportMerge = new GReportMerge();
				parseDrToStamp(dr, gReportMerge);
				list.add(gReportMerge);
			}
		}
		return list;
	}

	/**
	 * 根据用户id获取文件封面尾页名称
	 */
	public  DataTable getUserCoverEndStampPicList(String userId) throws Exception{
		StringBuffer s = new StringBuffer("select g_report_merge.NAME from g_user_cor_stamp LEFT OUTER JOIN g_report_merge "+
				" ON g_user_cor_stamp.USER_AUTHORIZATION_ID = g_report_merge.ID"+
				" where g_user_cor_stamp.USER_ID='"+userId+"'AND g_user_cor_stamp.AUTHORIZATION_TYPE=3");
		Sql sql  = new Sql(s.toString());
		return SqlSessionManager.getDataTable(sql);
	}
	
	public String getFilePathByMergeId(String id,String fieldName){
		 IStore store = StoreManager.create("g_report_merge");  
		 IObject   object = store.get(id);           
		 if(object!=null){
			 return object.getString(fieldName);
		 } 
		 return "";
	}
	
	public ViewFondreReportMerge ViewFondreReportMerge(String reportMergeId){
		ViewFondreReportMerge view = new ViewFondreReportMerge();
		String version = SystemHelper.getSystemSimpleVersion();
		String name="";
		String coverFileName="";
		String footerFileName="";
		String storageAddressId="";

		if (abc.util.StringUtils.isNullOrEmpty(reportMergeId)) {
		} else {
			//传id进来,显示相关信息
			 IStore store = StoreManager.create("g_report_merge");  //传入数据库表名
			 IObject   object = store.get(reportMergeId);           //传入ID   感觉id取得其他的值
			 if(object!=null){
				 name=object.getString("NAME");
				 coverFileName=object.getString("COVER_FILE_NAME");
				 footerFileName=object.getString("FOOTER_FILE_NAME");
				 storageAddressId=object.getString("STORAGE_ADDRESS_ID");

			 } 
		}
		view.setCoverFileName(coverFileName);
		view.setFooterFileName(footerFileName);
		view.setName(name);
		view.setReportMergeId(reportMergeId);
		view.setVersion(version);
		view.setStorageAddressId(storageAddressId);
		return view;
	}
}
